package com.slipper.common.constants;

import javax.servlet.http.HttpServletRequest;

import com.slipper.common.bo.RSAKeyVO;
import com.slipper.common.util.GsonUtil;
import com.slipper.common.util.RSAUtil;
import com.slipper.common.util.StringUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Objects;

public class Constants {
    /** 当前时间变量名 */
    public static final String CURR_TIME_NAME = "curTime";
    /** 数据变量名 */
    public static final String DATA_NAME = "data";
    /** 数据返回编码 */
    public static final String CODE_NAME = "code";
    /** 数据返回消息内容 */
    public static final String MSG_NAME = "message";
    /** 页码 */
    public static final String PAGE_NUMBER_NAME = "pageNumber";
    /** 页面大小 */
    public static final String PAGE_SIZE_NAME = "pageSize";
    /** 总页数 */
    public static final String TOTAL_PAGE_NAME = "totalPage";
    /** 总记录数 */
    public static final String TOTAL_RECORD_NAME = "totalRecord";

    public static final String CURRENT_EMPLOYEE_SESSION = "employeeSessionId";
    public static final String CURRENT_MEMBER_SESSION = "memberSessionId";
    public static final String CURRENT_CAPTCHA = "currentCaptcha";
    public static final String CURRENT_RSA_KEY = "currentRSAKey";

    /** 有效 */
    public static final String VALID = "1";
    /** 无效 */
    public static final String INVALID = "0";

    /** 是 */
    public static final String YES = "1";
    /** 否 */
    public static final String NO = "0";

    public static final String ROOT_ID = "0";

    /** 默认编码 */
    public static final String UTF_8 = "utf-8";

    public static HttpServletRequest getCurrentRequest() {
        return ((ServletRequestAttributes)
                Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }

    /**
     * 设置当前验证码
     */
    public static void setCaptcha(String captcha) {
        if (captcha == null) {
            getCurrentRequest().getSession().removeAttribute(CURRENT_CAPTCHA);
        } else {
            getCurrentRequest().getSession().setAttribute(CURRENT_CAPTCHA, captcha);
        }
    }

    /**
     * 获取当前验证码
     */
    public static String getAndCleanCaptcha() {
        String captcha = (String) getCurrentRequest().getSession().getAttribute(CURRENT_CAPTCHA);
        getCurrentRequest().getSession().setAttribute(CURRENT_CAPTCHA, null);
        return captcha;
    }

    /**
     * 获取RSAKeyVO
     */
    public static RSAKeyVO getRSAKey() {
        try {
            String json = (String) getCurrentRequest().getSession().getAttribute(CURRENT_RSA_KEY);
            if (StringUtil.isBlank(json)) {
                RSAKeyVO vo = RSAUtil.createKey();
                getCurrentRequest().getSession().setAttribute(CURRENT_RSA_KEY, GsonUtil.toJson(vo));
                return vo;
            } else {
                return GsonUtil.fromJson(json, RSAKeyVO.class);
            }
        } catch (Exception e) {
            throw new RuntimeException("无法生成RSA密钥对！", e);
        }
    }
}
